{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### Content provided under a Creative Commons Attribution license, CC-BY 4.0; code under BSD 3-Clause license. (c)2014 Lorena A. Barba, Olivier Mesnard. Thanks: NSF for support via CAREER award #1149784."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Infinite row of vortices"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The objective of this assignment is to visualize the streamlines around an infinite row of vortices. First, you will consider the case of a finite number of vortices, obtained by simple superposition. By adding more and more vortices, you should be able to see how the flow pattern approaches that of an infinite row of vortices. But there will always be some differences (pay attention to what these may be).\n",
    "\n",
    "It's possible to derive an analytical expression for the infinite case, and the derivation is provided below. With this analytical expression, you can visualize the streamlines for the infinite case. Observe and think: how are the streamlines different from one case to the other?\n",
    "\n",
    "\n",
    "In this notebook, there is no Python code. Your job is to study the theory (and follow the mathematics on your own handwritten notes), to think how you could implement it in an efficient manner and finally to code it and visualize the results."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Vortex flow (from previous lesson)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You might not suspect it, but the vortex has a very important role in classical aerodynamic theory. You'll discover some of its uses in this assignment.\n",
    "\n",
    "First, a little review of the basics. As seen in a previous lesson, a vortex of strength $\\Gamma$ has a stream-function:\n",
    "\n",
    "$$\\psi\\left(r,\\theta\\right) = \\frac{\\Gamma}{2\\pi}\\ln r$$\n",
    "\n",
    "and a velocity potential\n",
    "\n",
    "$$\\phi\\left(r,\\theta\\right) = -\\frac{\\Gamma}{2\\pi}\\theta$$\n",
    "\n",
    "We can now derive the velocity components in a polar coordinate system, as follows:\n",
    "\n",
    "$$u_r\\left(r,\\theta\\right) = 0$$\n",
    "\n",
    "$$u_\\theta\\left(r,\\theta\\right) = -\\frac{\\Gamma}{2\\pi r}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In a Cartesian coordinate system, the velocity components at $\\left(x,y\\right)$ around a vortex of strength $\\Gamma$ located at $\\left(x_\\text{vortex},y_\\text{vortex}\\right)$, are given by\n",
    "\n",
    "$$u\\left(x,y\\right) = +\\frac{\\Gamma}{2\\pi}\\frac{y-y_\\text{vortex}}{(x-x_\\text{vortex})^2+(y-y_\\text{vortex})^2}$$\n",
    "\n",
    "$$v\\left(x,y\\right) = -\\frac{\\Gamma}{2\\pi}\\frac{x-x_\\text{vortex}}{(x-x_\\text{vortex})^2+(y-y_\\text{vortex})^2}$$\n",
    "\n",
    "and the stream-function is written as\n",
    "\n",
    "$$\\psi\\left(x,y\\right) = \\frac{\\Gamma}{4\\pi}\\ln\\left((x-x_\\text{vortex})^2+(y-y_\\text{vortex})^2\\right)$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Superposition of many vortices"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this assignment, we consider a useful example to illustrate the concept of a *vortex sheet*: an infinite row of vortices of equal strength $\\Gamma$ (same sign and magnitude) evenly spaced by a distance $a$. But let's start with a finite row of vortices first.\n",
    "\n",
    "\n",
    "The stream-function $\\psi_i$ of the $i^{th}$ vortex at a distance $r_i$ is given by:\n",
    "\n",
    "$$\\psi_i = \\frac{\\Gamma}{2\\pi}\\ln r_i$$\n",
    "\n",
    "Applying the principle of superposition, the stream-function of $N$ vortices is, then\n",
    "\n",
    "$$\\psi = \\frac{\\Gamma}{2\\pi} \\sum_{i=1}^N \\ln r_i$$\n",
    "\n",
    "And the velocity field (in Cartesian coordinates) of the row of vortices is\n",
    "\n",
    "$$u\\left(x,y\\right) = + \\frac{\\Gamma}{2\\pi} \\sum_{i=1}^N \\frac{y-y_i}{(x-x_i)^2+(y-y_i)^2}$$\n",
    "\n",
    "$$v\\left(x,y\\right) = - \\frac{\\Gamma}{2\\pi} \\sum_{i=1}^N \\frac{x-x_i}{(x-x_i)^2+(y-y_i)^2}$$\n",
    "\n",
    "where $\\left(x_i,y_i\\right)$ are the Cartesian coordinates of the $i^{\\text{th}}$ vortex.\n",
    "\n",
    "Here is a diagram of the situation:\n",
    "\n",
    "<center><img src=\"resources/infiniteRowVorticesSketch.png\"></center>\n",
    "\n",
    "(In the next section, we'll show you how to derive a closed-form expression for the *infinite* sum of vortices. But hang on.)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Task:\n",
    "\n",
    "#### Compute the velocity field and plot the streamlines of a row of vortices."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You will place $N$ vortices aligned on the horizontal axis and visualize the flow pattern. Do the following:\n",
    "\n",
    "* Using the equations derived above, compute the velocity components of each vortex on a mesh grid.\n",
    "\n",
    "* Remember that a finite number of vortices can be represented by a *list* or a NumPy *array*. Think and decide which one to use.\n",
    "\n",
    "* Define functions to avoid code repetition  (and why not classes, if you prefer and are familiar with the concept).\n",
    "\n",
    "* Once you have all the velocities, apply the principle of superposition and plot the resulting flow pattern. \n",
    "\n",
    "* Play around with the size of your vortex row and the plotting range. Make your plot of publishable quality!\n",
    "\n",
    "In the end you should get something similar to this:\n",
    "\n",
    "<center><img src=\"resources/infiniteRowVortices.png\"></center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Infinite row of vortices"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This will be a fun mathematical exercise! Follow along, and take your own notes.\n",
    "\n",
    "To derive the closed-form functions, we use the complex representation:\n",
    "\n",
    "$$z = x + jy$$\n",
    "\n",
    "where $j^2=-1$. (We don't use $i$ to not confuse it with our indices!)\n",
    "\n",
    "The complex potential is defined as $w = \\phi + j\\psi$, where $\\phi$ is the potential velocity and $\\psi$ is the stream-function. If we differentiate the complex potential $w$ with respect to the complex variable $z$, we get the complex velocity\n",
    "\n",
    "$$\\frac{dw}{dz} = u-jv$$\n",
    "\n",
    "where $u$ and $v$ are the Cartesian velocity components.\n",
    "\n",
    "The complex potential representing a vortex of strength $\\Gamma$ located at the origin is\n",
    "\n",
    "$$w = \\frac{j\\Gamma}{2\\pi}\\ln z$$\n",
    "\n",
    "Why?\n",
    "\n",
    "Because $z=re^{j\\theta}$ and $w$ becomes\n",
    "\n",
    "$$w = -\\frac{\\Gamma}{2\\pi}\\theta + j\\frac{\\Gamma}{2\\pi}\\ln r = \\phi + j\\psi$$\n",
    "\n",
    "Consider a second vortex located at $\\left(a,0\\right)$ with the same strength $\\Gamma$. Its complex potential is given by\n",
    "\n",
    "$$w = \\frac{j\\Gamma}{2\\pi}\\ln \\left(z-a\\right)$$\n",
    "\n",
    "A additional vortex located $a$ further will have a complex potential of the form\n",
    "\n",
    "$$w = \\frac{j\\Gamma}{2\\pi}\\ln \\left(z-2a\\right)$$\n",
    "\n",
    "and so on...\n",
    "\n",
    "Therefore, the complex potential representing an infinite row of vortices (on the line $y=0$) is given by\n",
    "\n",
    "$$w = \\frac{j\\Gamma}{2\\pi}\\sum_{m=-\\infty}^{+\\infty} \\ln \\left( z-ma \\right)$$\n",
    "\n",
    "When we integrate the velocity components, in the previous notebooks, to find the stream-function and the potential, we dropped the constant of integration. Here, we decide to add a constant (not a random one!). Why not, since the differentiation will give the same results.\n",
    "\n",
    "$$w = \\frac{j\\Gamma}{2\\pi}\\sum_{m=-\\infty}^{+\\infty} \\ln \\left( z-ma \\right) + \\text{constant}$$\n",
    "\n",
    "where\n",
    "\n",
    "$$\\text{constant} = -\\frac{j\\Gamma}{2\\pi} \\sum_{m=-\\infty, m\\neq 0}^{+\\infty} \\ln \\left( -ma \\right)$$\n",
    "\n",
    "so that, the complex potential can be cast in the following form\n",
    "\n",
    "$$w = \\frac{j\\Gamma}{2\\pi}\\sum_{m=-\\infty,m\\neq 0}^{+\\infty} \\ln \\left( \\frac{z-ma}{-ma} \\right) + \\frac{j\\Gamma}{2\\pi}\\ln z$$\n",
    "\n",
    "Now, it is time do some mathematics...\n",
    "\n",
    "$$w = \\frac{j\\Gamma}{2\\pi}\\sum_{m=-\\infty,m\\neq 0}^{+\\infty} \\ln \\left( 1-\\frac{z}{ma} \\right) + \\frac{j\\Gamma}{2\\pi}\\ln z$$\n",
    "\n",
    "$$w = \\frac{j\\Gamma}{2\\pi}\\sum_{m=1}^{+\\infty} \\left\\lbrace \\ln\\left(1-\\frac{z}{ma}\\right) + \\ln\\left(1+\\frac{z}{ma}\\right) \\right\\rbrace + \\frac{j\\Gamma}{2\\pi}\\ln z$$\n",
    "\n",
    "$$w = \\frac{j\\Gamma}{2\\pi}\\sum_{m=1}^{+\\infty} \\ln\\left(1-\\frac{z^2}{m^2a^2}\\right) + \\frac{j\\Gamma}{2\\pi}\\ln z$$\n",
    "\n",
    "$$w = \\frac{j\\Gamma}{2\\pi} \\ln\\left(\\prod_{m=1}^{+\\infty}\\left(1-\\frac{z^2}{m^2a^2}\\right)\\right) + \\frac{j\\Gamma}{2\\pi}\\ln z$$\n",
    "\n",
    "$$w = \\frac{j\\Gamma}{2\\pi} \\ln\\left(z\\prod_{m=1}^{+\\infty}\\left(1-\\frac{z^2}{m^2a^2}\\right)\\right)$$\n",
    "\n",
    "$$w = \\frac{j\\Gamma}{2\\pi} \\ln\\left(z\\prod_{m=1}^{+\\infty}\\left(1-\\frac{\\left(\\frac{z\\pi}{a}\\right)^2}{m^2\\pi^2}\\right)\\right)$$\n",
    "\n",
    "The product is actually the formula for the sine and the complex potential becomes\n",
    "\n",
    "$$w = \\frac{j\\Gamma}{2\\pi}\\ln\\left(\\sin\\left(\\frac{z\\pi}{a}\\right)\\right)$$\n",
    "\n",
    "We derive the complex potential with respect to the complex variable to get the complex velocity\n",
    "\n",
    "$$\\frac{dw}{dz} = u-iv = \\frac{j\\Gamma}{2a}\\cot\\left(\\frac{z\\pi}{a}\\right)$$\n",
    "\n",
    "$$u-jv = \\frac{j\\Gamma}{2a}\\frac{\\cos\\left(\\frac{\\pi x}{a}+j\\frac{\\pi y}{a}\\right)}{\\sin\\left(\\frac{\\pi x}{a}+j\\frac{\\pi y}{a}\\right)}$$\n",
    "\n",
    "Applying trigonometric identities, we find the following expression\n",
    "\n",
    "$$u-jv = \\frac{j\\Gamma}{2a}\\frac{\\cos\\left(\\frac{\\pi x}{a}\\right)\\cosh\\left(\\frac{\\pi y}{a}\\right) - j\\sin\\left(\\frac{\\pi x}{a}\\right)\\sinh\\left(\\frac{\\pi y}{a}\\right)}{\\sin\\left(\\frac{\\pi x}{a}\\right)\\cosh\\left(\\frac{\\pi y}{a}\\right) + j\\cos\\left(\\frac{\\pi x}{a}\\right)\\sinh\\left(\\frac{\\pi y}{a}\\right)}$$\n",
    "\n",
    "which can be cast in the form (try it!)\n",
    "\n",
    "$$u-jv = \\frac{\\Gamma}{2a}\\frac{\\sinh\\left(\\frac{2\\pi y}{a}\\right)}{\\cosh\\left(\\frac{2\\pi y}{a}\\right) - \\cos\\left(\\frac{2\\pi x}{a}\\right)} + j \\frac{\\Gamma}{2a}\\frac{\\sin\\left(\\frac{2\\pi x}{a}\\right)}{\\cosh\\left(\\frac{2\\pi y}{a}\\right) - \\cos\\left(\\frac{2\\pi x}{a}\\right)}$$\n",
    "\n",
    "Therefore, the Cartesian velocity components of an infinite row of vortices are given by\n",
    "\n",
    "$$u\\left(x,y\\right) = +\\frac{\\Gamma}{2a}\\frac{\\sinh\\left(\\frac{2\\pi y}{a}\\right)}{\\cosh\\left(\\frac{2\\pi y}{a}\\right)-\\cos\\left(\\frac{2\\pi x}{a}\\right)}$$\n",
    "\n",
    "$$v\\left(x,y\\right) = -\\frac{\\Gamma}{2a}\\frac{\\sin\\left(\\frac{2\\pi x}{a}\\right)}{\\cosh\\left(\\frac{2\\pi y}{a}\\right)-\\cos\\left(\\frac{2\\pi x}{a}\\right)}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Task:\n",
    "\n",
    "#### Compute the velocity field and plot the streamlines of an *infinite* row of vortices"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now that we have derived the functions for the velocity components, implement them in a code cell and plot the streamlines. \n",
    "\n",
    "*Can you notice the differences with the previous case where the number of vortices was finite?*\n",
    "\n",
    "Play around with your plotting settings for the *finite* row of vortices, until you can get a plot that looks more like the infinite case. When can you say the finite case is a good approximation of the infinite case?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Think"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice that the streamline pattern is parallel to the *vortex sheet*: there is no net normal flow. Therefore, the sheet can model a solid surface in potential flow. We end up with a slip velocity at such an interface: how is this consistent with potential flow? "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Please ignore the cell below. It just loads our style for the notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<link href='http://fonts.googleapis.com/css?family=Fenix' rel='stylesheet' type='text/css'>\n",
       "<link href='http://fonts.googleapis.com/css?family=Alegreya+Sans:100,300,400,500,700,800,900,100italic,300italic,400italic,500italic,700italic,800italic,900italic' rel='stylesheet' type='text/css'>\n",
       "<link href='http://fonts.googleapis.com/css?family=Source+Code+Pro:300,400' rel='stylesheet' type='text/css'>\n",
       "<style>\n",
       "\n",
       "@font-face {\n",
       "    font-family: \"Computer Modern\";\n",
       "    src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunss.otf');\n",
       "}\n",
       "\n",
       "\n",
       "#notebook_panel { /* main background */\n",
       "    background: rgb(245,245,245);\n",
       "}\n",
       "\n",
       "div.cell { /* set cell width */\n",
       "    width: 750px;\n",
       "}\n",
       "\n",
       "div #notebook { /* centre the content */\n",
       "    background: #fff; /* white background for content */\n",
       "    width: 1000px;\n",
       "    margin: auto;\n",
       "    padding-left: 0em;\n",
       "}\n",
       "\n",
       "#notebook li { /* More space between bullet points */\n",
       "margin-top:0.8em;\n",
       "}\n",
       "\n",
       "/* draw border around running cells */\n",
       "div.cell.border-box-sizing.code_cell.running { \n",
       "    border: 1px solid #111;\n",
       "}\n",
       "\n",
       "/* Put a solid color box around each cell and its output, visually linking them*/\n",
       "div.cell.code_cell {\n",
       "    background-color: rgb(256,256,256); \n",
       "    border-radius: 0px; \n",
       "    padding: 0.5em;\n",
       "    margin-left:1em;\n",
       "    margin-top: 1em;\n",
       "}\n",
       "\n",
       "\n",
       "div.text_cell_render{\n",
       "    font-family: 'Alegreya Sans' sans-serif;\n",
       "    line-height: 140%;\n",
       "    font-size: 125%;\n",
       "    font-weight: 400;\n",
       "    width:600px;\n",
       "    margin-left:auto;\n",
       "    margin-right:auto;\n",
       "}\n",
       "\n",
       "/* Formatting for header cells */\n",
       ".text_cell_render h1 {\n",
       "    font-family: 'Alegreya Sans', sans-serif;\n",
       "    font-style:regular;\n",
       "    font-weight: 200;    \n",
       "    font-size: 50pt;\n",
       "    line-height: 100%;\n",
       "    color:#CD2305;\n",
       "    margin-bottom: 0.5em;\n",
       "    margin-top: 0.5em;\n",
       "    display: block;\n",
       "}\t\n",
       ".text_cell_render h2 {\n",
       "    font-family: 'Fenix', serif;\n",
       "    font-size: 22pt;\n",
       "    line-height: 100%;\n",
       "    margin-bottom: 0.1em;\n",
       "    margin-top: 0.3em;\n",
       "    display: block;\n",
       "}\t\n",
       "\n",
       ".text_cell_render h3 {\n",
       "    font-family: 'Fenix', serif;\n",
       "    margin-top:12px;\n",
       "\tfont-size: 16pt;\n",
       "    margin-bottom: 3px;\n",
       "    font-style: regular;\n",
       "}\n",
       "\n",
       ".text_cell_render h4 {    /*Use this for captions*/\n",
       "    font-family: 'Fenix', serif;\n",
       "    font-size: 2pt;\n",
       "    text-align: center;\n",
       "    margin-top: 0em;\n",
       "    margin-bottom: 2em;\n",
       "    font-style: regular;\n",
       "}\n",
       "\n",
       ".text_cell_render h5 {  /*Use this for small titles*/\n",
       "    font-family: 'Alegreya Sans', sans-serif;\n",
       "    font-weight: 300;\n",
       "    font-size: 16pt;\n",
       "    color: #CD2305;\n",
       "    font-style: italic;\n",
       "    margin-bottom: .5em;\n",
       "    margin-top: 0.5em;\n",
       "    display: block;\n",
       "}\n",
       "\n",
       ".text_cell_render h6 { /*use this for copyright note*/\n",
       "    font-family: 'Source Code Pro', sans-serif;\n",
       "    font-weight: 300;\n",
       "    font-size: 9pt;\n",
       "    line-height: 100%;\n",
       "    color: grey;\n",
       "    margin-bottom: 1px;\n",
       "    margin-top: 1px;\n",
       "}\n",
       "\n",
       "    .CodeMirror{\n",
       "            font-family: \"Source Code Pro\";\n",
       "\t\t\tfont-size: 90%;\n",
       "    }\n",
       "/*    .prompt{\n",
       "        display: None;\n",
       "    }*/\n",
       "\t\n",
       "    \n",
       "    .warning{\n",
       "        color: rgb( 240, 20, 20 )\n",
       "        }  \n",
       "</style>\n",
       "<script>\n",
       "    MathJax.Hub.Config({\n",
       "                        TeX: {\n",
       "                           extensions: [\"AMSmath.js\"], \n",
       "                           equationNumbers: { autoNumber: \"AMS\", useLabelIds: true}\n",
       "                           },\n",
       "                tex2jax: {\n",
       "                    inlineMath: [ ['$','$'], [\"\\\\(\",\"\\\\)\"] ],\n",
       "                    displayMath: [ ['$$','$$'], [\"\\\\[\",\"\\\\]\"] ]\n",
       "                },\n",
       "                displayAlign: 'center', // Change this to 'center' to center equations.\n",
       "                \"HTML-CSS\": {\n",
       "                    styles: {'.MathJax_Display': {\"margin\": 4}}\n",
       "                }\n",
       "        });\n",
       "</script>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from IPython.core.display import HTML\n",
    "def css_styling(filepath):\n",
    "    styles = open(filepath, 'r').read()\n",
    "    return HTML(styles)\n",
    "css_styling('../styles/custom.css')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
